{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas.util.testing as tm; tm.N = 3\n",
    "def unpivot(frame):\n",
    "    N, K = frame.shape\n",
    "    data = {'value' : frame.values.ravel('F'),\n",
    "            'variable' : np.asarray(frame.columns).repeat(N),\n",
    "            'date' : np.tile(np.asarray(frame.index), K)}\n",
    "    return pd.DataFrame(data, columns=['date', 'variable', 'value'])\n",
    "df = unpivot(tm.makeTimeDataFrame())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>A</td>\n",
       "      <td>-0.166550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>A</td>\n",
       "      <td>-1.540176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>A</td>\n",
       "      <td>0.931574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>B</td>\n",
       "      <td>1.045622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>B</td>\n",
       "      <td>0.808190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>B</td>\n",
       "      <td>0.358843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>C</td>\n",
       "      <td>-0.146395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>C</td>\n",
       "      <td>0.389329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>C</td>\n",
       "      <td>-1.102814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>D</td>\n",
       "      <td>-0.073086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>D</td>\n",
       "      <td>1.538478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>D</td>\n",
       "      <td>0.035537</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date variable     value\n",
       "0  2000-01-03        A -0.166550\n",
       "1  2000-01-04        A -1.540176\n",
       "2  2000-01-05        A  0.931574\n",
       "3  2000-01-03        B  1.045622\n",
       "4  2000-01-04        B  0.808190\n",
       "5  2000-01-05        B  0.358843\n",
       "6  2000-01-03        C -0.146395\n",
       "7  2000-01-04        C  0.389329\n",
       "8  2000-01-05        C -1.102814\n",
       "9  2000-01-03        D -0.073086\n",
       "10 2000-01-04        D  1.538478\n",
       "11 2000-01-05        D  0.035537"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>variable</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>-0.166550</td>\n",
       "      <td>1.045622</td>\n",
       "      <td>-0.146395</td>\n",
       "      <td>-0.073086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>-1.540176</td>\n",
       "      <td>0.808190</td>\n",
       "      <td>0.389329</td>\n",
       "      <td>1.538478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>0.931574</td>\n",
       "      <td>0.358843</td>\n",
       "      <td>-1.102814</td>\n",
       "      <td>0.035537</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "variable           A         B         C         D\n",
       "date                                              \n",
       "2000-01-03 -0.166550  1.045622 -0.146395 -0.073086\n",
       "2000-01-04 -1.540176  0.808190  0.389329  1.538478\n",
       "2000-01-05  0.931574  0.358843 -1.102814  0.035537"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_df=df.pivot(index='date',columns='variable',values='value')\n",
    "pivot_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>-0.166550</td>\n",
       "      <td>1.045622</td>\n",
       "      <td>-0.146395</td>\n",
       "      <td>-0.073086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>-1.540176</td>\n",
       "      <td>0.808190</td>\n",
       "      <td>0.389329</td>\n",
       "      <td>1.538478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>0.931574</td>\n",
       "      <td>0.358843</td>\n",
       "      <td>-1.102814</td>\n",
       "      <td>0.035537</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date         A         B         C         D\n",
       "0 2000-01-03 -0.166550  1.045622 -0.146395 -0.073086\n",
       "1 2000-01-04 -1.540176  0.808190  0.389329  1.538478\n",
       "2 2000-01-05  0.931574  0.358843 -1.102814  0.035537"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_df=df.pivot(index='date',columns='variable',values='value')\n",
    "pivot_df.columns = ['_'.join(col).strip() for col in pivot_df.columns.values]\n",
    "pivot_df.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "      <th>value2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>A</td>\n",
       "      <td>-0.166550</td>\n",
       "      <td>-0.333100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>A</td>\n",
       "      <td>-1.540176</td>\n",
       "      <td>-3.080352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>A</td>\n",
       "      <td>0.931574</td>\n",
       "      <td>1.863148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>B</td>\n",
       "      <td>1.045622</td>\n",
       "      <td>2.091244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>B</td>\n",
       "      <td>0.808190</td>\n",
       "      <td>1.616380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>B</td>\n",
       "      <td>0.358843</td>\n",
       "      <td>0.717685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>C</td>\n",
       "      <td>-0.146395</td>\n",
       "      <td>-0.292791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>C</td>\n",
       "      <td>0.389329</td>\n",
       "      <td>0.778657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>C</td>\n",
       "      <td>-1.102814</td>\n",
       "      <td>-2.205628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>D</td>\n",
       "      <td>-0.073086</td>\n",
       "      <td>-0.146173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>D</td>\n",
       "      <td>1.538478</td>\n",
       "      <td>3.076956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>D</td>\n",
       "      <td>0.035537</td>\n",
       "      <td>0.071074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date variable     value    value2\n",
       "0  2000-01-03        A -0.166550 -0.333100\n",
       "1  2000-01-04        A -1.540176 -3.080352\n",
       "2  2000-01-05        A  0.931574  1.863148\n",
       "3  2000-01-03        B  1.045622  2.091244\n",
       "4  2000-01-04        B  0.808190  1.616380\n",
       "5  2000-01-05        B  0.358843  0.717685\n",
       "6  2000-01-03        C -0.146395 -0.292791\n",
       "7  2000-01-04        C  0.389329  0.778657\n",
       "8  2000-01-05        C -1.102814 -2.205628\n",
       "9  2000-01-03        D -0.073086 -0.146173\n",
       "10 2000-01-04        D  1.538478  3.076956\n",
       "11 2000-01-05        D  0.035537  0.071074"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['value2'] = df['value']*2\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"4\" halign=\"left\">value</th>\n",
       "      <th colspan=\"4\" halign=\"left\">value2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>variable</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>-0.166550</td>\n",
       "      <td>1.045622</td>\n",
       "      <td>-0.146395</td>\n",
       "      <td>-0.073086</td>\n",
       "      <td>-0.333100</td>\n",
       "      <td>2.091244</td>\n",
       "      <td>-0.292791</td>\n",
       "      <td>-0.146173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>-1.540176</td>\n",
       "      <td>0.808190</td>\n",
       "      <td>0.389329</td>\n",
       "      <td>1.538478</td>\n",
       "      <td>-3.080352</td>\n",
       "      <td>1.616380</td>\n",
       "      <td>0.778657</td>\n",
       "      <td>3.076956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>0.931574</td>\n",
       "      <td>0.358843</td>\n",
       "      <td>-1.102814</td>\n",
       "      <td>0.035537</td>\n",
       "      <td>1.863148</td>\n",
       "      <td>0.717685</td>\n",
       "      <td>-2.205628</td>\n",
       "      <td>0.071074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               value                                  value2            \\\n",
       "variable           A         B         C         D         A         B   \n",
       "date                                                                     \n",
       "2000-01-03 -0.166550  1.045622 -0.146395 -0.073086 -0.333100  2.091244   \n",
       "2000-01-04 -1.540176  0.808190  0.389329  1.538478 -3.080352  1.616380   \n",
       "2000-01-05  0.931574  0.358843 -1.102814  0.035537  1.863148  0.717685   \n",
       "\n",
       "                                \n",
       "variable           C         D  \n",
       "date                            \n",
       "2000-01-03 -0.292791 -0.146173  \n",
       "2000-01-04  0.778657  3.076956  \n",
       "2000-01-05 -2.205628  0.071074  "
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_df = df.pivot(index='date',columns='variable')\n",
    "pivot_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>value_A</th>\n",
       "      <th>value_B</th>\n",
       "      <th>value_C</th>\n",
       "      <th>value_D</th>\n",
       "      <th>value2_A</th>\n",
       "      <th>value2_B</th>\n",
       "      <th>value2_C</th>\n",
       "      <th>value2_D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>-0.166550</td>\n",
       "      <td>1.045622</td>\n",
       "      <td>-0.146395</td>\n",
       "      <td>-0.073086</td>\n",
       "      <td>-0.333100</td>\n",
       "      <td>2.091244</td>\n",
       "      <td>-0.292791</td>\n",
       "      <td>-0.146173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>-1.540176</td>\n",
       "      <td>0.808190</td>\n",
       "      <td>0.389329</td>\n",
       "      <td>1.538478</td>\n",
       "      <td>-3.080352</td>\n",
       "      <td>1.616380</td>\n",
       "      <td>0.778657</td>\n",
       "      <td>3.076956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>0.931574</td>\n",
       "      <td>0.358843</td>\n",
       "      <td>-1.102814</td>\n",
       "      <td>0.035537</td>\n",
       "      <td>1.863148</td>\n",
       "      <td>0.717685</td>\n",
       "      <td>-2.205628</td>\n",
       "      <td>0.071074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date   value_A   value_B   value_C   value_D  value2_A  value2_B  \\\n",
       "0 2000-01-03 -0.166550  1.045622 -0.146395 -0.073086 -0.333100  2.091244   \n",
       "1 2000-01-04 -1.540176  0.808190  0.389329  1.538478 -3.080352  1.616380   \n",
       "2 2000-01-05  0.931574  0.358843 -1.102814  0.035537  1.863148  0.717685   \n",
       "\n",
       "   value2_C  value2_D  \n",
       "0 -0.292791 -0.146173  \n",
       "1  0.778657  3.076956  \n",
       "2 -2.205628  0.071074  "
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_df.columns = ['_'.join(col).strip() for col in pivot_df.columns.values]\n",
    "pivot_df.reset_index(level=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## stack = make stuff vertically stacked\n",
    "## unstack = spread stuff out horizontally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>exp</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>animal</th>\n",
       "      <th>cat</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>hair_length</th>\n",
       "      <th>long</th>\n",
       "      <th>long</th>\n",
       "      <th>short</th>\n",
       "      <th>short</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.243153</td>\n",
       "      <td>-1.712484</td>\n",
       "      <td>-0.989127</td>\n",
       "      <td>1.664282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.589307</td>\n",
       "      <td>0.721118</td>\n",
       "      <td>-1.424156</td>\n",
       "      <td>0.135460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.162633</td>\n",
       "      <td>-0.516633</td>\n",
       "      <td>0.099211</td>\n",
       "      <td>0.254756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.987366</td>\n",
       "      <td>-1.008657</td>\n",
       "      <td>-2.541929</td>\n",
       "      <td>-0.065774</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "exp                 A         B         A         B\n",
       "animal            cat       cat       dog       dog\n",
       "hair_length      long      long     short     short\n",
       "0            0.243153 -1.712484 -0.989127  1.664282\n",
       "1            0.589307  0.721118 -1.424156  0.135460\n",
       "2           -0.162633 -0.516633  0.099211  0.254756\n",
       "3            0.987366 -1.008657 -2.541929 -0.065774"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns = pd.MultiIndex.from_tuples([\n",
    "            ('A', 'cat', 'long'), ('B', 'cat', 'long'),\n",
    "            ('A', 'dog', 'short'), ('B', 'dog', 'short')\n",
    "        ],\n",
    "        names=['exp', 'animal', 'hair_length']\n",
    "    )\n",
    "df = pd.DataFrame(np.random.randn(4, 4), columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['A', 'B'], ['cat', 'dog'], ['long', 'short']],\n",
       "           labels=[[0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]],\n",
       "           names=['exp', 'animal', 'hair_length'])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>hair_length</th>\n",
       "      <th>long</th>\n",
       "      <th>short</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>exp</th>\n",
       "      <th>animal</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.243153</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.989127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <td>-1.712484</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.664282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.589307</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.424156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.721118</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.135460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">2</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <td>-0.162633</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.099211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <td>-0.516633</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.254756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">3</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.987366</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-2.541929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <td>-1.008657</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.065774</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "hair_length       long     short\n",
       "  exp animal                    \n",
       "0 A   cat     0.243153       NaN\n",
       "      dog          NaN -0.989127\n",
       "  B   cat    -1.712484       NaN\n",
       "      dog          NaN  1.664282\n",
       "1 A   cat     0.589307       NaN\n",
       "      dog          NaN -1.424156\n",
       "  B   cat     0.721118       NaN\n",
       "      dog          NaN  0.135460\n",
       "2 A   cat    -0.162633       NaN\n",
       "      dog          NaN  0.099211\n",
       "  B   cat    -0.516633       NaN\n",
       "      dog          NaN  0.254756\n",
       "3 A   cat     0.987366       NaN\n",
       "      dog          NaN -2.541929\n",
       "  B   cat    -1.008657       NaN\n",
       "      dog          NaN -0.065774"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stacked = df.stack(level=['exp','animal'])\n",
    "stacked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>animal</th>\n",
       "      <th>hair_length</th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>0.243153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>-0.989127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>-1.712484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>1.664282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>0.589307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>-1.424156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>0.721118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>0.135460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>-0.162633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>0.099211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>-0.516633</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>0.254756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>0.987366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>-2.541929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>cat</td>\n",
       "      <td>long</td>\n",
       "      <td>-1.008657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>dog</td>\n",
       "      <td>short</td>\n",
       "      <td>-0.065774</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   animal hair_length         0\n",
       "0     cat        long  0.243153\n",
       "1     dog       short -0.989127\n",
       "2     cat        long -1.712484\n",
       "3     dog       short  1.664282\n",
       "4     cat        long  0.589307\n",
       "5     dog       short -1.424156\n",
       "6     cat        long  0.721118\n",
       "7     dog       short  0.135460\n",
       "8     cat        long -0.162633\n",
       "9     dog       short  0.099211\n",
       "10    cat        long -0.516633\n",
       "11    dog       short  0.254756\n",
       "12    cat        long  0.987366\n",
       "13    dog       short -2.541929\n",
       "14    cat        long -1.008657\n",
       "15    dog       short -0.065774"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stacked.reset_index(level=[0,1],drop=True).stack().to_frame().reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## missing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "columns = pd.MultiIndex.from_tuples([('A', 'cat'), ('B', 'dog'),\n",
    "                                         ('B', 'cat'), ('A', 'dog')],\n",
    "                                        names=['exp', 'animal'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "index = pd.MultiIndex.from_product([('bar', 'baz', 'foo', 'qux'),\n",
    "                                        ('one', 'two')],\n",
    "                                       names=['first', 'second'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>exp</th>\n",
       "      <th>A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>animal</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.331016</td>\n",
       "      <td>1.492836</td>\n",
       "      <td>-0.850707</td>\n",
       "      <td>-2.194239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.170267</td>\n",
       "      <td>-0.825463</td>\n",
       "      <td>0.075396</td>\n",
       "      <td>-0.020315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.134711</td>\n",
       "      <td>-0.139876</td>\n",
       "      <td>-2.525406</td>\n",
       "      <td>-1.073265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.759569</td>\n",
       "      <td>-0.849516</td>\n",
       "      <td>-0.598635</td>\n",
       "      <td>0.162714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.560310</td>\n",
       "      <td>-0.309663</td>\n",
       "      <td>2.585012</td>\n",
       "      <td>-1.697059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-1.908190</td>\n",
       "      <td>1.027783</td>\n",
       "      <td>0.189191</td>\n",
       "      <td>-0.461268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.300106</td>\n",
       "      <td>-1.834859</td>\n",
       "      <td>-0.989717</td>\n",
       "      <td>-1.022173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.618693</td>\n",
       "      <td>0.423368</td>\n",
       "      <td>0.839311</td>\n",
       "      <td>-0.516362</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "exp                  A         B                   A\n",
       "animal             cat       dog       cat       dog\n",
       "first second                                        \n",
       "bar   one     0.331016  1.492836 -0.850707 -2.194239\n",
       "      two    -0.170267 -0.825463  0.075396 -0.020315\n",
       "baz   one    -0.134711 -0.139876 -2.525406 -1.073265\n",
       "      two     1.759569 -0.849516 -0.598635  0.162714\n",
       "foo   one    -1.560310 -0.309663  2.585012 -1.697059\n",
       "      two    -1.908190  1.027783  0.189191 -0.461268\n",
       "qux   one     0.300106 -1.834859 -0.989717 -1.022173\n",
       "      two     1.618693  0.423368  0.839311 -0.516362"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>exp</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>animal</th>\n",
       "      <th>dog</th>\n",
       "      <th>cat</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>1.492836</td>\n",
       "      <td>-0.850707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.825463</td>\n",
       "      <td>0.075396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.309663</td>\n",
       "      <td>2.585012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>qux</th>\n",
       "      <th>two</th>\n",
       "      <td>0.423368</td>\n",
       "      <td>0.839311</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "exp                  B          \n",
       "animal             dog       cat\n",
       "first second                    \n",
       "bar   one     1.492836 -0.850707\n",
       "      two    -0.825463  0.075396\n",
       "foo   one    -0.309663  2.585012\n",
       "qux   two     0.423368  0.839311"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.iloc[[0, 1, 4, 7], [1, 2]]\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>exp</th>\n",
       "      <th>A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>animal</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.331016</td>\n",
       "      <td>1.492836</td>\n",
       "      <td>-0.850707</td>\n",
       "      <td>-2.194239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.170267</td>\n",
       "      <td>-0.825463</td>\n",
       "      <td>0.075396</td>\n",
       "      <td>-0.020315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.134711</td>\n",
       "      <td>-0.139876</td>\n",
       "      <td>-2.525406</td>\n",
       "      <td>-1.073265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.759569</td>\n",
       "      <td>-0.849516</td>\n",
       "      <td>-0.598635</td>\n",
       "      <td>0.162714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.560310</td>\n",
       "      <td>-0.309663</td>\n",
       "      <td>2.585012</td>\n",
       "      <td>-1.697059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-1.908190</td>\n",
       "      <td>1.027783</td>\n",
       "      <td>0.189191</td>\n",
       "      <td>-0.461268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.300106</td>\n",
       "      <td>-1.834859</td>\n",
       "      <td>-0.989717</td>\n",
       "      <td>-1.022173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.618693</td>\n",
       "      <td>0.423368</td>\n",
       "      <td>0.839311</td>\n",
       "      <td>-0.516362</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "exp                  A         B                   A\n",
       "animal             cat       dog       cat       dog\n",
       "first second                                        \n",
       "bar   one     0.331016  1.492836 -0.850707 -2.194239\n",
       "      two    -0.170267 -0.825463  0.075396 -0.020315\n",
       "baz   one    -0.134711 -0.139876 -2.525406 -1.073265\n",
       "      two     1.759569 -0.849516 -0.598635  0.162714\n",
       "foo   one    -1.560310 -0.309663  2.585012 -1.697059\n",
       "      two    -1.908190  1.027783  0.189191 -0.461268\n",
       "qux   one     0.300106 -1.834859 -0.989717 -1.022173\n",
       "      two     1.618693  0.423368  0.839311 -0.516362"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  groupby + reshaping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>exp</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>animal</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">bar</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.331016</td>\n",
       "      <td>-0.850707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-2.194239</td>\n",
       "      <td>1.492836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">two</th>\n",
       "      <th>cat</th>\n",
       "      <td>-0.170267</td>\n",
       "      <td>0.075396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-0.020315</td>\n",
       "      <td>-0.825463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">baz</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>cat</th>\n",
       "      <td>-0.134711</td>\n",
       "      <td>-2.525406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-1.073265</td>\n",
       "      <td>-0.139876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">two</th>\n",
       "      <th>cat</th>\n",
       "      <td>1.759569</td>\n",
       "      <td>-0.598635</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>0.162714</td>\n",
       "      <td>-0.849516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">foo</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>cat</th>\n",
       "      <td>-1.560310</td>\n",
       "      <td>2.585012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-1.697059</td>\n",
       "      <td>-0.309663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">two</th>\n",
       "      <th>cat</th>\n",
       "      <td>-1.908190</td>\n",
       "      <td>0.189191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-0.461268</td>\n",
       "      <td>1.027783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">qux</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>cat</th>\n",
       "      <td>0.300106</td>\n",
       "      <td>-0.989717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-1.022173</td>\n",
       "      <td>-1.834859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">two</th>\n",
       "      <th>cat</th>\n",
       "      <td>1.618693</td>\n",
       "      <td>0.839311</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>-0.516362</td>\n",
       "      <td>0.423368</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "exp                         A         B\n",
       "first second animal                    \n",
       "bar   one    cat     0.331016 -0.850707\n",
       "             dog    -2.194239  1.492836\n",
       "      two    cat    -0.170267  0.075396\n",
       "             dog    -0.020315 -0.825463\n",
       "baz   one    cat    -0.134711 -2.525406\n",
       "             dog    -1.073265 -0.139876\n",
       "      two    cat     1.759569 -0.598635\n",
       "             dog     0.162714 -0.849516\n",
       "foo   one    cat    -1.560310  2.585012\n",
       "             dog    -1.697059 -0.309663\n",
       "      two    cat    -1.908190  0.189191\n",
       "             dog    -0.461268  1.027783\n",
       "qux   one    cat     0.300106 -0.989717\n",
       "             dog    -1.022173 -1.834859\n",
       "      two    cat     1.618693  0.839311\n",
       "             dog    -0.516362  0.423368"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>animal</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.259846</td>\n",
       "      <td>-0.350702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.047435</td>\n",
       "      <td>-0.422889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-1.330059</td>\n",
       "      <td>-0.606571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.580467</td>\n",
       "      <td>-0.343401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>0.512351</td>\n",
       "      <td>-1.003361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-0.859499</td>\n",
       "      <td>0.283257</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.344806</td>\n",
       "      <td>-1.428516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>1.229002</td>\n",
       "      <td>-0.046497</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "animal             cat       dog\n",
       "first second                    \n",
       "bar   one    -0.259846 -0.350702\n",
       "      two    -0.047435 -0.422889\n",
       "baz   one    -1.330059 -0.606571\n",
       "      two     0.580467 -0.343401\n",
       "foo   one     0.512351 -1.003361\n",
       "      two    -0.859499  0.283257\n",
       "qux   one    -0.344806 -1.428516\n",
       "      two     1.229002 -0.046497"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack().mean(1).unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The function pivot_table() can be used to create spreadsheet-style pivot tables. See the cookbook for some advanced strategies.\n",
    "\n",
    "> pivot_table = tabela dinâmica!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Global TF Kernel (Python 3)",
   "language": "python",
   "name": "global-tf-python-3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
